home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gp_mswin.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  22.0 KB  |  862 lines

  1. /* Copyright (C) 1992, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gp_mswin.c,v 1.3 2000/09/19 19:00:24 lpd Exp $ */
  20. /*
  21.  * Microsoft Windows 3.n platform support for Ghostscript.
  22.  *
  23.  * Original version by Russell Lang and Maurice Castro with help from
  24.  * Programming Windows, 2nd Ed., Charles Petzold, Microsoft Press;
  25.  * initially created from gp_dosfb.c and gp_itbc.c 5th June 1992.
  26.  */
  27.  
  28. /* Modified for Win32 & Microsoft C/C++ 8.0 32-Bit, 26.Okt.1994 */
  29. /* by Friedrich Nowak                                           */
  30.  
  31. /* Original EXE and GSview specific code removed */
  32. /* DLL version must now be used under MS-Windows */
  33. /* Russell Lang 16 March 1996 */
  34.  
  35. #include "stdio_.h"
  36. #include "string_.h"
  37. #include "memory_.h"
  38. #include <stdlib.h>
  39. #include <stdarg.h>
  40. #include "ctype_.h"
  41. #include "dos_.h"
  42. #include <io.h>
  43. #include "malloc_.h"
  44. #include <fcntl.h>
  45. #include <signal.h>
  46. #include "gx.h"
  47. #include "gp.h"
  48. #include "gpcheck.h"
  49. #include "gpmisc.h"
  50. #include "gserrors.h"
  51. #include "gsexit.h"
  52.  
  53. #include "windows_.h"
  54. #include <shellapi.h>
  55. #ifdef __WIN32__
  56. #include <winspool.h>
  57. #endif
  58. #include "gp_mswin.h"
  59. #include "gsdll.h"
  60. /* use longjmp instead of exit when using DLL */
  61. #include <setjmp.h>
  62. extern jmp_buf gsdll_env;
  63.  
  64. /* Library routines not declared in a standard header */
  65. extern char *getenv(P1(const char *));
  66.  
  67. /* ------ from gnuplot winmain.c plus new stuff ------ */
  68.  
  69. /* limits */
  70. #define MAXSTR 255
  71.  
  72. /* public handles */
  73. HINSTANCE phInstance;
  74. HWND hwndtext = HWND_DESKTOP;    /* would be better to be a real window */
  75.  
  76. const LPSTR szAppName = "Ghostscript";
  77. BOOL is_win32s = FALSE;
  78. char FAR win_prntmp[MAXSTR];    /* filename of PRN temporary file */
  79. private int is_printer(const char *name);
  80. int win_init = 0;        /* flag to know if gp_exit has been called */
  81. int win_exit_status;
  82.  
  83. BOOL CALLBACK _export AbortProc(HDC, int);
  84.  
  85. #ifdef __WIN32__
  86. /* DLL entry point for Borland C++ */
  87. BOOL WINAPI _export
  88. DllEntryPoint(HINSTANCE hInst, DWORD fdwReason, LPVOID lpReserved)
  89. {
  90.     /* Win32s: HIWORD bit 15 is 1 and bit 14 is 0 */
  91.     /* Win95:  HIWORD bit 15 is 1 and bit 14 is 1 */
  92.     /* WinNT:  HIWORD bit 15 is 0 and bit 14 is 0 */
  93.     /* WinNT Shell Update Release is WinNT && LOBYTE(LOWORD) >= 4 */
  94.     DWORD version = GetVersion();
  95.  
  96.     if (((HIWORD(version) & 0x8000) != 0) && ((HIWORD(version) & 0x4000) == 0))
  97.     is_win32s = TRUE;
  98.  
  99.     phInstance = hInst;
  100.     return TRUE;
  101. }
  102.  
  103. /* DLL entry point for Microsoft Visual C++ */
  104. BOOL WINAPI _export
  105. DllMain(HINSTANCE hInst, DWORD fdwReason, LPVOID lpReserved)
  106. {
  107.     return DllEntryPoint(hInst, fdwReason, lpReserved);
  108. }
  109.  
  110.  
  111. #else
  112. int WINAPI _export
  113. LibMain(HINSTANCE hInstance, WORD wDataSeg, WORD wHeapSize, LPSTR lpszCmdLine)
  114. {
  115.     phInstance = hInstance;
  116.     return 1;
  117. }
  118.  
  119. int WINAPI _export
  120. WEP(int nParam)
  121. {
  122.     return 1;
  123. }
  124. #endif
  125.  
  126.  
  127. BOOL CALLBACK _export
  128. AbortProc(HDC hdcPrn, int code)
  129. {
  130.     process_interrupts();
  131.     if (code == SP_OUTOFDISK)
  132.     return (FALSE);        /* cancel job */
  133.     return (TRUE);
  134. }
  135.  
  136. /* ------ Process message loop ------ */
  137. /*
  138.  * Check messages and interrupts; return true if interrupted.
  139.  * This is called frequently - it must be quick!
  140.  */
  141. int
  142. gp_check_interrupts(void)
  143. {
  144.     return (*pgsdll_callback) (GSDLL_POLL, NULL, 0);
  145. }
  146.  
  147. /* ====== Generic platform procedures ====== */
  148.  
  149. /* ------ Initialization/termination (from gp_itbc.c) ------ */
  150.  
  151. /* Do platform-dependent initialization. */
  152. void
  153. gp_init(void)
  154. {
  155.     win_init = 1;
  156. }
  157.  
  158. /* Do platform-dependent cleanup. */
  159. void
  160. gp_exit(int exit_status, int code)
  161. {
  162.     win_init = 0;
  163.     win_exit_status = exit_status;
  164. }
  165.  
  166. /* Exit the program. */
  167. void
  168. gp_do_exit(int exit_status)
  169. {
  170.     /* Use longjmp since exit would terminate caller */
  171.     /* setjmp code will check gs_exit_status */
  172.     longjmp(gsdll_env, gs_exit_status);
  173. }
  174.  
  175. /* ------ Printer accessing ------ */
  176.  
  177. /* Forward references */
  178. private int gp_printfile(P2(const char *, const char *));
  179.  
  180. /* Open a connection to a printer.  A null file name means use the */
  181. /* standard printer connected to the machine, if any. */
  182. /* Return NULL if the connection could not be opened. */
  183. FILE *
  184. gp_open_printer(char fname[gp_file_name_sizeof], int binary_mode)
  185. {
  186.     if (is_printer(fname)) {
  187.     FILE *pfile;
  188.  
  189.     /* Open a scratch file, which we will send to the */
  190.     /* actual printer in gp_close_printer. */
  191.     pfile = gp_open_scratch_file(gp_scratch_file_name_prefix,
  192.                      win_prntmp, "wb");
  193.     return pfile;
  194.     } else
  195.     return fopen(fname, (binary_mode ? "wb" : "w"));
  196. }
  197.  
  198. /* Close the connection to the printer. */
  199. void
  200. gp_close_printer(FILE * pfile, const char *fname)
  201. {
  202.     fclose(pfile);
  203.     if (!is_printer(fname))
  204.     return;            /* a file, not a printer */
  205.  
  206.     gp_printfile(win_prntmp, fname);
  207.     unlink(win_prntmp);
  208. }
  209.  
  210. /* Printer abort procedure and progress/cancel dialog box */
  211. /* Used by Win32 and mswinprn device */
  212.  
  213. HWND hDlgModeless;
  214.  
  215. BOOL CALLBACK _export
  216. PrintAbortProc(HDC hdcPrn, int code)
  217. {
  218.     MSG msg;
  219.  
  220.     while (hDlgModeless && PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
  221.     if (hDlgModeless || !IsDialogMessage(hDlgModeless, &msg)) {
  222.         TranslateMessage(&msg);
  223.         DispatchMessage(&msg);
  224.     }
  225.     }
  226.     return (hDlgModeless != 0);
  227. }
  228.  
  229. /* Modeless dialog box - Cancel printing */
  230. BOOL CALLBACK _export
  231. CancelDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  232. {
  233.     switch (message) {
  234.     case WM_INITDIALOG:
  235.         SetWindowText(hDlg, szAppName);
  236.         return TRUE;
  237.     case WM_COMMAND:
  238.         switch (LOWORD(wParam)) {
  239.         case IDCANCEL:
  240.             DestroyWindow(hDlg);
  241.             hDlgModeless = 0;
  242.             EndDialog(hDlg, 0);
  243.             return TRUE;
  244.         }
  245.     }
  246.     return FALSE;
  247. }
  248.  
  249. #ifndef __WIN32__
  250.  
  251. /* Windows does not provide API's in the SDK for writing directly to a */
  252. /* printer.  Instead you are supposed to use the Windows printer drivers. */
  253. /* Ghostscript has its own printer drivers, so we need to use some API's */
  254. /* that are documented only in the Device Driver Adaptation Guide */
  255. /* that comes with the DDK.  Prototypes taken from DDK <print.h> */
  256. DECLARE_HANDLE(HPJOB);
  257.  
  258. HPJOB WINAPI OpenJob(LPSTR, LPSTR, HPJOB);
  259. int WINAPI StartSpoolPage(HPJOB);
  260. int WINAPI EndSpoolPage(HPJOB);
  261. int WINAPI WriteSpool(HPJOB, LPSTR, int);
  262. int WINAPI CloseJob(HPJOB);
  263. int WINAPI DeleteJob(HPJOB, int);
  264. int WINAPI WriteDialog(HPJOB, LPSTR, int);
  265. int WINAPI DeleteSpoolPage(HPJOB);
  266.  
  267. #endif /* WIN32 */
  268.  
  269. /* Dialog box to select printer port */
  270. BOOL CALLBACK _export
  271. SpoolDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  272. {
  273.     LPSTR entry;
  274.  
  275.     switch (message) {
  276.     case WM_INITDIALOG:
  277.         entry = (LPSTR) lParam;
  278.         while (*entry) {
  279.         SendDlgItemMessage(hDlg, SPOOL_PORT, LB_ADDSTRING, 0, (LPARAM) entry);
  280.         entry += lstrlen(entry) + 1;
  281.         }
  282.         SendDlgItemMessage(hDlg, SPOOL_PORT, LB_SETCURSEL, 0, (LPARAM) 0);
  283.         return TRUE;
  284.     case WM_COMMAND:
  285.         switch (LOWORD(wParam)) {
  286.         case SPOOL_PORT:
  287. #ifdef __WIN32__
  288.             if (HIWORD(wParam)
  289. #else
  290.             if (HIWORD(lParam)
  291. #endif
  292.             == LBN_DBLCLK)
  293.             PostMessage(hDlg, WM_COMMAND, IDOK, 0L);
  294.             return FALSE;
  295.         case IDOK:
  296.             EndDialog(hDlg, 1 + (int)SendDlgItemMessage(hDlg, SPOOL_PORT, LB_GETCURSEL, 0, 0L));
  297.             return TRUE;
  298.         case IDCANCEL:
  299.             EndDialog(hDlg, 0);
  300.             return TRUE;
  301.         }
  302.     }
  303.     return FALSE;
  304. }
  305.  
  306. /* return TRUE if queue looks like a valid printer name */
  307. int
  308. is_spool(const char *queue)
  309. {
  310.     char *prefix = "\\\\spool";    /* 7 characters long */
  311.     int i;
  312.  
  313.     for (i = 0; i < 7; i++) {
  314.     if (prefix[i] == '\\') {
  315.         if ((*queue != '\\') && (*queue != '/'))
  316.         return FALSE;
  317.     } else if (tolower(*queue) != prefix[i])
  318.         return FALSE;
  319.     queue++;
  320.     }
  321.     if (*queue && (*queue != '\\') && (*queue != '/'))
  322.     return FALSE;
  323.     return TRUE;
  324. }
  325.  
  326.  
  327. private int
  328. is_printer(const char *name)
  329. {
  330.     char buf[128];
  331.  
  332.     /* is printer if no name given */
  333.     if (strlen(name) == 0)
  334.     return TRUE;
  335.  
  336.     /*  is printer if name appears in win.ini [ports] section */
  337.     GetProfileString("ports", name, "XYZ", buf, sizeof(buf));
  338.     if (strlen(name) == 0 || strcmp(buf, "XYZ"))
  339.     return TRUE;
  340.  
  341.     /* is printer if name prefixed by \\spool\ */
  342.     if (is_spool(name))
  343.     return TRUE;
  344.  
  345.     return FALSE;
  346. }
  347.  
  348. #ifdef __WIN32__        /* ******** WIN32 ******** */
  349.  
  350. /******************************************************************/
  351. /* Print File to port or queue */
  352. /* port==NULL means prompt for port or queue with dialog box */
  353.  
  354. /* This is messy because Microsoft changed the spooler interface */
  355. /* between Window 3.1 and Windows 95/NT */
  356. /* and didn't provide the spooler interface in Win32s */
  357.  
  358. /* Win95, WinNT: Use OpenPrinter, WritePrinter etc. */
  359. private int gp_printfile_win32(const char *filename, char *port);
  360.  
  361. /* Win32s: Pass to Win16 spooler via gs16spl.exe */
  362. private int gp_printfile_gs16spl(const char *filename, const char *port);
  363.  
  364. /*
  365.  * Valid values for pmport are:
  366.  *   ""
  367.  *      action: WinNT and Win95 use default queue, Win32s prompts for port
  368.  *   "LPT1:" (or other port that appears in win.ini [ports]
  369.  *      action: start gs16spl.exe to print to the port
  370.  *   "\\spool\printer name"
  371.  *      action: send to printer using WritePrinter (WinNT and Win95).
  372.  *      action: translate to port name using win.ini [Devices]
  373.  *              then use gs16spl.exe (Win32s).
  374.  *   "\\spool"
  375.  *      action: prompt for queue name then send to printer using 
  376.  *              WritePrinter (WinNT and Win95).
  377.  *      action: prompt for port then use gs16spl.exe (Win32s).
  378.  */
  379. /* Print File */
  380. private int
  381. gp_printfile(const char *filename, const char *pmport)
  382. {
  383.     /* treat WinNT and Win95 differently to Win32s */
  384.     if (!is_win32s) {
  385.     if (strlen(pmport) == 0) {    /* get default printer */
  386.         char buf[256];
  387.         char *p;
  388.  
  389.         /* WinNT stores default printer in registry and win.ini */
  390.         /* Win95 stores default printer in win.ini */
  391.         GetProfileString("windows", "device", "", buf, sizeof(buf));
  392.         if ((p = strchr(buf, ',')) != NULL)
  393.         *p = '\0';
  394.         return gp_printfile_win32(filename, buf);
  395.     } else if (is_spool(pmport)) {
  396.         if (strlen(pmport) >= 8)
  397.         return gp_printfile_win32(filename, (char *)pmport + 8);
  398.         else
  399.         return gp_printfile_win32(filename, (char *)NULL);
  400.     } else
  401.         return gp_printfile_gs16spl(filename, pmport);
  402.     } else {
  403.     /* Win32s */
  404.     if (is_spool(pmport)) {
  405.         if (strlen(pmport) >= 8) {
  406.         /* extract port name from win.ini */
  407.         char driverbuf[256];
  408.         char *output;
  409.  
  410.         GetProfileString("Devices", pmport + 8, "", driverbuf, sizeof(driverbuf));
  411.         strtok(driverbuf, ",");
  412.         output = strtok(NULL, ",");
  413.         return gp_printfile_gs16spl(filename, output);
  414.         } else
  415.         return gp_printfile_gs16spl(filename, (char *)NULL);
  416.     } else
  417.         return gp_printfile_gs16spl(filename, pmport);
  418.     }
  419. }
  420.  
  421. #define PRINT_BUF_SIZE 16384u
  422. #define PORT_BUF_SIZE 4096
  423.  
  424. char *
  425. get_queues(void)
  426. {
  427.     int i;
  428.     DWORD count, needed;
  429.     PRINTER_INFO_1 *prinfo;
  430.     char *enumbuffer;
  431.     char *buffer;
  432.     char *p;
  433.  
  434.     /* enumerate all available printers */
  435.     EnumPrinters(PRINTER_ENUM_CONNECTIONS | PRINTER_ENUM_LOCAL, NULL, 1, NULL, 0, &needed, &count);
  436.     if (needed == 0) {
  437.     /* no printers */
  438.     enumbuffer = malloc(4);
  439.     if (enumbuffer == (char *)NULL)
  440.         return NULL;
  441.     memset(enumbuffer, 0, 4);
  442.     return enumbuffer;
  443.     }
  444.     enumbuffer = malloc(needed);
  445.     if (enumbuffer == (char *)NULL)
  446.     return NULL;
  447.     if (!EnumPrinters(PRINTER_ENUM_CONNECTIONS | PRINTER_ENUM_LOCAL, NULL, 1, (LPBYTE) enumbuffer, needed, &needed, &count)) {
  448.     char buf[256];
  449.  
  450.     free(enumbuffer);
  451.     sprintf(buf, "EnumPrinters() failed, error code = %d", GetLastError());
  452.     MessageBox((HWND) NULL, buf, szAppName, MB_OK | MB_ICONSTOP);
  453.     return NULL;
  454.     }
  455.     prinfo = (PRINTER_INFO_1 *) enumbuffer;
  456.     if ((buffer = malloc(PORT_BUF_SIZE)) == (char *)NULL) {
  457.     free(enumbuffer);
  458.     return NULL;
  459.     }
  460.     /* copy printer names to single buffer */
  461.     p = buffer;
  462.     for (i = 0; i < count; i++) {
  463.     if (strlen(prinfo[i].pName) + 1 < (PORT_BUF_SIZE - (p - buffer))) {
  464.         strcpy(p, prinfo[i].pName);
  465.         p += strlen(p) + 1;
  466.     }
  467.     }
  468.     *p = '\0';            /* double null at end */
  469.     free(enumbuffer);
  470.     return buffer;
  471. }
  472.  
  473.  
  474. char *
  475. get_ports(void)
  476. {
  477.     char *buffer;
  478.  
  479. #ifdef __WIN32__
  480.     if (!is_win32s)
  481.     return get_queues();
  482. #endif
  483.  
  484.     if ((buffer = malloc(PORT_BUF_SIZE)) == (char *)NULL)
  485.     return NULL;
  486.     GetProfileString("ports", NULL, "", buffer, PORT_BUF_SIZE);
  487.     return buffer;
  488. }
  489.  
  490. /* return TRUE if queuename available */
  491. /* return FALSE if cancelled or error */
  492. /* if queue non-NULL, use as suggested queue */
  493. BOOL
  494. get_queuename(char *portname, const char *queue)
  495. {
  496.     char *buffer;
  497.     char *p;
  498.     int i, iport;
  499.  
  500.     buffer = get_queues();
  501.     if (buffer == NULL)
  502.     return FALSE;
  503.     if ((queue == (char *)NULL) || (strlen(queue) == 0)) {
  504.     /* select a queue */
  505.     iport = DialogBoxParam(phInstance, "QueueDlgBox", (HWND) NULL, SpoolDlgProc, (LPARAM) buffer);
  506.     if (!iport) {
  507.         free(buffer);
  508.         return FALSE;
  509.     }
  510.     p = buffer;
  511.     for (i = 1; i < iport && strlen(p) != 0; i++)
  512.         p += lstrlen(p) + 1;
  513.     /* prepend \\spool\ which is used by Ghostscript to distinguish */
  514.     /* real files from queues */
  515.     strcpy(portname, "\\\\spool\\");
  516.     strcat(portname, p);
  517.     } else {
  518.     strcpy(portname, "\\\\spool\\");
  519.     strcat(portname, queue);
  520.     }
  521.  
  522.     free(buffer);
  523.     return TRUE;
  524. }
  525.  
  526. /* return TRUE if portname available */
  527. /* return FALSE if cancelled or error */
  528. /* if port non-NULL, use as suggested port */
  529. BOOL
  530. get_portname(char *portname, const char *port)
  531. {
  532.     char *buffer;
  533.     char *p;
  534.     int i, iport;
  535.     char filename[MAXSTR];
  536.  
  537.     buffer = get_ports();
  538.     if (buffer == NULL)
  539.     return FALSE;
  540.     if ((port == (char *)NULL) || (strlen(port) == 0)) {
  541.     if (buffer == (char *)NULL)
  542.         return FALSE;
  543.     /* select a port */
  544.     iport = DialogBoxParam(phInstance, "SpoolDlgBox", (HWND) NULL, SpoolDlgProc, (LPARAM) buffer);
  545.     if (!iport) {
  546.         free(buffer);
  547.         return FALSE;
  548.     }
  549.     p = buffer;
  550.     for (i = 1; i < iport && strlen(p) != 0; i++)
  551.         p += lstrlen(p) + 1;
  552.     strcpy(portname, p);
  553.     } else
  554.     strcpy(portname, port);
  555.  
  556.     if (strlen(portname) == 0)
  557.     return FALSE;
  558.     if (strcmp(portname, "FILE:") == 0) {
  559.     OPENFILENAME ofn;
  560.  
  561.     filename[0] = '\0';
  562.     memset(&ofn, 0, sizeof(OPENFILENAME));
  563.     ofn.lStructSize = sizeof(OPENFILENAME);
  564.     ofn.hwndOwner = (HWND) NULL;
  565.     ofn.lpstrFile = filename;
  566.     ofn.nMaxFile = sizeof(filename);
  567.     ofn.Flags = OFN_PATHMUSTEXIST;
  568.     if (!GetSaveFileName(&ofn)) {
  569.         free(buffer);
  570.         return FALSE;
  571.     }
  572.     strcpy(portname, filename);
  573.     }
  574.     free(buffer);
  575.     return TRUE;
  576. }
  577.  
  578.  
  579. /* True Win32 method, using OpenPrinter, WritePrinter etc. */
  580. private int
  581. gp_printfile_win32(const char *filename, char *port)
  582. {
  583.     DWORD count;
  584.     char *buffer;
  585.     char portname[MAXSTR];
  586.     FILE *f;
  587.     HANDLE printer;
  588.     DOC_INFO_1 di;
  589.     DWORD written;
  590.  
  591.     if (!get_queuename(portname, port))
  592.     return FALSE;
  593.     port = portname + 8;    /* skip over \\spool\ */
  594.  
  595.     if ((buffer = malloc(PRINT_BUF_SIZE)) == (char *)NULL)
  596.     return FALSE;
  597.  
  598.     if ((f = fopen(filename, "rb")) == (FILE *) NULL) {
  599.     free(buffer);
  600.     return FALSE;
  601.     }
  602.     /* open a printer */
  603.     if (!OpenPrinter(port, &printer, NULL)) {
  604.     char buf[256];
  605.  
  606.     sprintf(buf, "OpenPrinter() failed for \042%s\042, error code = %d", port, GetLastError());
  607.     MessageBox((HWND) NULL, buf, szAppName, MB_OK | MB_ICONSTOP);
  608.     free(buffer);
  609.     return FALSE;
  610.     }
  611.     /* from here until ClosePrinter, should AbortPrinter on error */
  612.  
  613.     di.pDocName = szAppName;
  614.     di.pOutputFile = NULL;
  615.     di.pDatatype = "RAW";    /* for available types see EnumPrintProcessorDatatypes */
  616.     if (!StartDocPrinter(printer, 1, (LPBYTE) & di)) {
  617.     char buf[256];
  618.  
  619.     sprintf(buf, "StartDocPrinter() failed, error code = %d", GetLastError());
  620.     MessageBox((HWND) NULL, buf, szAppName, MB_OK | MB_ICONSTOP);
  621.     AbortPrinter(printer);
  622.     free(buffer);
  623.     return FALSE;
  624.     }
  625.     /* copy file to printer */
  626.     while ((count = fread(buffer, 1, PRINT_BUF_SIZE, f)) != 0) {
  627.     if (!WritePrinter(printer, (LPVOID) buffer, count, &written)) {
  628.         free(buffer);
  629.         fclose(f);
  630.         AbortPrinter(printer);
  631.         return FALSE;
  632.     }
  633.     }
  634.     fclose(f);
  635.     free(buffer);
  636.  
  637.     if (!EndDocPrinter(printer)) {
  638.     char buf[256];
  639.  
  640.     sprintf(buf, "EndDocPrinter() failed, error code = %d", GetLastError());
  641.     MessageBox((HWND) NULL, buf, szAppName, MB_OK | MB_ICONSTOP);
  642.     AbortPrinter(printer);
  643.     return FALSE;
  644.     }
  645.     if (!ClosePrinter(printer)) {
  646.     char buf[256];
  647.  
  648.     sprintf(buf, "ClosePrinter() failed, error code = %d", GetLastError());
  649.     MessageBox((HWND) NULL, buf, szAppName, MB_OK | MB_ICONSTOP);
  650.     return FALSE;
  651.     }
  652.     return TRUE;
  653. }
  654.  
  655.  
  656. /* Start a 16-bit application gs16spl.exe to print a file */
  657. /* Intended for Win32s where 16-bit spooler functions are not available */
  658. /* and Win32 spooler functions are not implemented. */
  659. int
  660. gp_printfile_gs16spl(const char *filename, const char *port)
  661. {
  662. /* Get printer port list from win.ini */
  663.     char portname[MAXSTR];
  664.     HINSTANCE hinst;
  665.     char command[MAXSTR];
  666.     char *p;
  667.     HWND hwndspl;
  668.  
  669.     if (!get_portname(portname, port))
  670.     return FALSE;
  671.  
  672.     /* get path to EXE - same as DLL */
  673.     GetModuleFileName(phInstance, command, sizeof(command));
  674.     if ((p = strrchr(command, '\\')) != (char *)NULL)
  675.     p++;
  676.     else
  677.     p = command;
  678.     *p = '\0';
  679.     sprintf(command + strlen(command), "gs16spl.exe %s %s",
  680.         portname, filename);
  681.  
  682.     hinst = (HINSTANCE) WinExec(command, SW_SHOWNORMAL);
  683.     if (hinst < (HINSTANCE) HINSTANCE_ERROR) {
  684.     char buf[MAXSTR];
  685.  
  686.     sprintf(buf, "Can't run: %s", command);
  687.     MessageBox((HWND) NULL, buf, szAppName, MB_OK | MB_ICONSTOP);
  688.     return FALSE;
  689.     }
  690.     hwndspl = FindWindow(NULL, "GS Win32s/Win16 spooler");
  691.  
  692.     while (IsWindow(hwndspl)) {
  693.     gp_check_interrupts();
  694.     }
  695.  
  696.     return 0;
  697. }
  698.  
  699.  
  700.  
  701. #else /* ******** !WIN32 ******** */
  702.  
  703. /* Print File to port */
  704. private int
  705. gp_printfile(const char *filename, const char *pmport)
  706. {
  707. #define PRINT_BUF_SIZE 16384u
  708.     char *buffer;
  709.     char *portname;
  710.     int i, port;
  711.     FILE *f;
  712.     DLGPROC lpfnSpoolProc;
  713.     WORD count;
  714.     DLGPROC lpfnCancelProc;
  715.     int error = FALSE;
  716.     long lsize;
  717.     long ldone;
  718.     char pcdone[20];
  719.     MSG msg;
  720.     HPJOB hJob;
  721.  
  722.     if (is_spool(pmport) && (strlen(pmport) >= 8)) {
  723.     /* translate from printer name to port name */
  724.     char driverbuf[256];
  725.  
  726.     GetProfileString("Devices", pmport + 8, "", driverbuf, sizeof(driverbuf));
  727.     strtok(driverbuf, ",");
  728.     pmport = strtok(NULL, ",");
  729.     }
  730.     /* get list of ports */
  731.     if ((buffer = malloc(PRINT_BUF_SIZE)) == (char *)NULL)
  732.     return FALSE;
  733.  
  734.     if ((strlen(pmport) == 0) || (strcmp(pmport, "PRN") == 0)) {
  735.     GetProfileString("ports", NULL, "", buffer, PRINT_BUF_SIZE);
  736.     /* select a port */
  737. #ifdef __WIN32__
  738.     lpfnSpoolProc = (DLGPROC) SpoolDlgProc;
  739. #else
  740. #ifdef __DLL__
  741.     lpfnSpoolProc = (DLGPROC) GetProcAddress(phInstance, "SpoolDlgProc");
  742. #else
  743.     lpfnSpoolProc = (DLGPROC) MakeProcInstance((FARPROC) SpoolDlgProc, phInstance);
  744. #endif
  745. #endif
  746.     port = DialogBoxParam(phInstance, "SpoolDlgBox", (HWND) NULL, lpfnSpoolProc, (LPARAM) buffer);
  747. #if !defined(__WIN32__) && !defined(__DLL__)
  748.     FreeProcInstance((FARPROC) lpfnSpoolProc);
  749. #endif
  750.     if (!port) {
  751.         free(buffer);
  752.         return FALSE;
  753.     }
  754.     portname = buffer;
  755.     for (i = 1; i < port && strlen(portname) != 0; i++)
  756.         portname += lstrlen(portname) + 1;
  757.     } else
  758.     portname = (char *)pmport;    /* Print Manager port name already supplied */
  759.  
  760.     if ((f = fopen(filename, "rb")) == (FILE *) NULL) {
  761.     free(buffer);
  762.     return FALSE;
  763.     }
  764.     fseek(f, 0L, SEEK_END);
  765.     lsize = ftell(f);
  766.     if (lsize <= 0)
  767.     lsize = 1;
  768.     fseek(f, 0L, SEEK_SET);
  769.  
  770.     hJob = OpenJob(portname, filename, (HPJOB) NULL);
  771.     switch ((int)hJob) {
  772.     case SP_APPABORT:
  773.     case SP_ERROR:
  774.     case SP_OUTOFDISK:
  775.     case SP_OUTOFMEMORY:
  776.     case SP_USERABORT:
  777.         fclose(f);
  778.         free(buffer);
  779.         return FALSE;
  780.     }
  781.     if (StartSpoolPage(hJob) < 0)
  782.     error = TRUE;
  783.  
  784. #ifdef __WIN32__
  785.     lpfnCancelProc = (DLGPROC) CancelDlgProc;
  786. #else
  787. #ifdef __DLL__
  788.     lpfnCancelProc = (DLGPROC) GetProcAddress(phInstance, "CancelDlgProc");
  789. #else
  790.     lpfnCancelProc = (DLGPROC) MakeProcInstance((FARPROC) CancelDlgProc, phInstance);
  791. #endif
  792. #endif
  793.     hDlgModeless = CreateDialog(phInstance, "CancelDlgBox", (HWND) NULL, lpfnCancelProc);
  794.     ldone = 0;
  795.  
  796.     while (!error && hDlgModeless
  797.        && (count = fread(buffer, 1, PRINT_BUF_SIZE, f)) != 0) {
  798.     if (WriteSpool(hJob, buffer, count) < 0)
  799.         error = TRUE;
  800.     ldone += count;
  801.     sprintf(pcdone, "%d %%done", (int)(ldone * 100 / lsize));
  802.     SetWindowText(GetDlgItem(hDlgModeless, CANCEL_PCDONE), pcdone);
  803.     while (PeekMessage(&msg, hDlgModeless, 0, 0, PM_REMOVE)) {
  804.         if ((hDlgModeless == 0) || !IsDialogMessage(hDlgModeless, &msg)) {
  805.         TranslateMessage(&msg);
  806.         DispatchMessage(&msg);
  807.         }
  808.     }
  809.     }
  810.     free(buffer);
  811.     fclose(f);
  812.  
  813.     if (!hDlgModeless)
  814.     error = TRUE;
  815.     DestroyWindow(hDlgModeless);
  816.     hDlgModeless = 0;
  817. #if !defined(__WIN32__) && !defined(__DLL__)
  818.     FreeProcInstance((FARPROC) lpfnCancelProc);
  819. #endif
  820.     EndSpoolPage(hJob);
  821.     if (error)
  822.     DeleteJob(hJob, 0);
  823.     else
  824.     CloseJob(hJob);
  825.     return !error;
  826. }
  827.  
  828. #endif /* ******** (!)WIN32 ******** */
  829.  
  830. /* ------ File naming and accessing ------ */
  831.  
  832. /* Create and open a scratch file with a given name prefix. */
  833. /* Write the actual file name at fname. */
  834. FILE *
  835. gp_open_scratch_file(const char *prefix, char *fname, const char *mode)
  836. {                /* The -7 is for XXXXXX plus a possible final \. */
  837.     int len = gp_file_name_sizeof - strlen(prefix) - 7;
  838.  
  839.     if (gp_gettmpdir(fname, &len) != 0)
  840.     *fname = 0;
  841.     else {
  842.     char *temp;
  843.  
  844.     /* Prevent X's in path from being converted by mktemp. */
  845.     for (temp = fname; *temp; temp++)
  846.         *temp = tolower(*temp);
  847.     if (strlen(fname) && (fname[strlen(fname) - 1] != '\\'))
  848.         strcat(fname, "\\");
  849.     }
  850.     strcat(fname, prefix);
  851.     strcat(fname, "XXXXXX");
  852.     mktemp(fname);
  853.     return gp_fopentemp(fname, mode);
  854. }
  855.  
  856. /* Open a file with the given name, as a stream of uninterpreted bytes. */
  857. FILE *
  858. gp_fopen(const char *fname, const char *mode)
  859. {
  860.     return fopen(fname, mode);
  861. }
  862.